import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

@Slf4j
public class T {
    public static void main(String[] args) {

        try (RandomAccessFile file = new RandomAccessFile("data.txt","rw")) {
            //获取文件通道
            FileChannel channel = file.getChannel();
            ByteBuffer buffer = ByteBuffer.allocate(10);
            while (true){
                int len = channel.read(buffer);
                log.info("读到的字节数：" + len);
                if (len == -1){
                    break;
                }
                buffer.flip(); //切换为读模式
                while (buffer.hasRemaining()){
                    byte b = buffer.get();
                    log.info("读到的数据：" + (char)b);
                }
                buffer.clear();//切换为写模式
            }
        } catch (IOException e) {}
    }
}
